package com.zkdcloud.controller.mobile;

import com.xdja.interceptor.annotation.BeforeProcess;
import com.zkdcloud.aspectInterceptor.advice.MobilePageAdvice;
import com.zkdcloud.aspectInterceptor.advice.MobileTokenAdvice;
import com.zkdcloud.domain.Business;
import com.zkdcloud.domain.User;
import com.zkdcloud.dto.FavoriteDisplay;
import com.zkdcloud.dto.TerminalResultsPage;
import com.zkdcloud.service.AdminService;
import com.zkdcloud.service.BusinessService;
import com.zkdcloud.service.UserService;
import org.jfaster.mango.plugin.page.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.zkdcloud.constant.Constants.HTTP_CONSTANTS.REQUEST_USER;

/**
 * 终端获取用户信息
 *
 * @author zk123
 * @version 2018/5/16
 */
@RestController
@RequestMapping(value = "/api")
@BeforeProcess(advice = MobileTokenAdvice.class)
public class MobileUserController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private UserService userService;

    @Autowired
    private BusinessService businessService;
    /**
     * 获取用户信息
     *
     * @param id id
     * @return userInfo
     */
    @RequestMapping(value = "/users/{id}", method = RequestMethod.GET)
    public Object getUser(@PathVariable String id) {
        User user = userService.getById(id);
        return user;
    }

    @RequestMapping(value = "/users/setNickName/{id}", method = RequestMethod.POST)
    public Object setNickName(@PathVariable String id, String nickname) {
        User user = new User();
        user.setId(id);
        user.setNickname(nickname);
        userService.updateUser(user);
        return userService.getById(id);
    }

    @BeforeProcess(advice = MobilePageAdvice.class)
    @RequestMapping(value = "/users/favorites", method = RequestMethod.GET)
    public Object getFavorites(Page page, Map<String,Object> map) {
        User user = (User) map.get(REQUEST_USER);
        List<FavoriteDisplay> tempTerminalBeans = businessService.getFavoriteDisplayList(user.getId(),page);
        Integer pageCount = page.getTotal() % page.getPageSize() == 0 ? page.getTotal() / page.getPageSize() : page.getTotal() / page.getPageSize() + 1;

        TerminalResultsPage<FavoriteDisplay> resultBean = new TerminalResultsPage(page.getTotal(), pageCount, page.getPageNum(), page.getPageSize());
        resultBean.setItems(tempTerminalBeans);
        return resultBean;
    }

    @RequestMapping(value = "/users/update/avatar", method = RequestMethod.PUT)
    public Object updateAvatar(User user) {
        userService.updateUser(user);
        return userService.getById(user.getId());
    }
}
